package com.link.play.fit.ai.ui.scanFood

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.link.play.fit.ai.bean.ScanIngredientBean
import com.link.play.fit.ai.databinding.ItemIngredientsBinding


class IngredientsAdapter() : RecyclerView.Adapter<IngredientsAdapter.SS66>() {

    var closeAction: ((bean: ScanIngredientBean) -> Unit)? = null
    var removeAction: (() -> Unit)? = null
    private var value: Double = 1.0

    private val list = arrayListOf<ScanIngredientBean>()

    inner class SS66(view: View) : RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SS66 {
        val bind =
            ItemIngredientsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return SS66(bind.root)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: SS66, position: Int) {
        val data = list[position]
        val binding = ItemIngredientsBinding.bind(holder.itemView)
        binding.tvContent.text = data.name
        binding.tvValue.text = (data.caloriesKcal * value).toInt().toString()
//        binding.ivClose.setOnClickListener {
//            closeAction?.invoke(data)
//            removeData(data)
//        }
        if (data.removed) {
            removeUI(data, binding)
        } else {
            unRemoveUI(data, binding)
        }

    }

    @SuppressLint("NotifyDataSetChanged")
    private fun unRemoveUI(data: ScanIngredientBean, binding: ItemIngredientsBinding) {
        binding.ivClose.visibility = View.VISIBLE
        binding.tvContent.setTextColor(Color.parseColor("#FF1D1A23"))
        binding.tvValue.setTextColor(Color.parseColor("#FF000000"))
        binding.tvValue.paintFlags = binding.tvValue.paintFlags and Paint.STRIKE_THRU_TEXT_FLAG.inv()
        binding.tvAdd.visibility = View.GONE
//        binding.ivClose.setOnClickListener {
//            data.removed = true
//            notifyDataSetChanged()
//            removeAction?.invoke()
//        }
        binding.root.setOnClickListener {
            data.removed = true
            notifyDataSetChanged()
            removeAction?.invoke()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun removeUI(data: ScanIngredientBean, binding: ItemIngredientsBinding) {
        binding.ivClose.visibility = View.GONE
        binding.tvContent.setTextColor(Color.parseColor("#FF8D8C92"))
        binding.tvValue.setTextColor(Color.parseColor("#FF8D8C92"))
        binding.tvValue.paintFlags = binding.tvValue.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
        binding.tvAdd.visibility = View.VISIBLE
//        binding.tvAdd.setOnClickListener {
//            data.removed = false
//            notifyDataSetChanged()
//            removeAction?.invoke()
//        }
        binding.root.setOnClickListener {
            data.removed = false
            notifyDataSetChanged()
            removeAction?.invoke()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun removeData(data: ScanIngredientBean) {
        list.remove(data)
        notifyDataSetChanged()
    }

    @SuppressLint("NotifyDataSetChanged")
    fun submit(subList: List<ScanIngredientBean>) {
        list.clear()
        list.addAll(subList)
        notifyDataSetChanged()
    }


    @SuppressLint("NotifyDataSetChanged")
    fun notifyMulValue(value: Double) {
        this.value = value
        notifyDataSetChanged()
    }
}